import Cookies from 'js-cookie'

const TokenKey = 'Admin-Token'

export function getToken () {
  return Cookies.get(TokenKey)
}

export function setToken (token) {
  return Cookies.set(TokenKey, token)
}

export function removeToken () {
  return Cookies.remove(TokenKey)
}

/**
 * 取消axios请求
 * @param cellArr 要取消的接口；如果不传参，则取消canCancelAxios保存的所有接口
 */
export function cancelAxios (cellArr = []) {
  if (cellArr.length > 0) {
    cellArr.forEach(val => {
      for (let i = 0; i < window.canCancelAxios.length; i++) {
        if (window.canCancelAxios[i].key === val) {
          window.canCancelAxios[i].cancel()
          window.canCancelAxios.splice(i, 1)
          i--
        }
      }
    })
  } else {
    for (let i = 0; i < window.canCancelAxios.length; i++) {
      window.canCancelAxios[i].cancel()
      window.canCancelAxios.splice(i, 1)
      i--
    }
  }
}
